<template>
    <div class="order-design" :custom="info.all_custom_count ? 'custom' : ''">
        <h3>
            <em>Product preview</em>
            <span v-if="info && info.order_state == 13">
                <svg-icon name="notify2" />
                <b>The order contains third-party platform products</b>
            </span>
        </h3>

        <div class="order-design-title">
            <span>Product</span>
            <span>Color/Size</span>
            <span>Designs</span>
            <span>Price</span>
            <span>Quantity</span>
            <span>Subtotal</span>
            <span v-if="info.all_custom_count">Edit layers</span>
        </div>

        <ul class="order-design-list">
            <li v-for="(item, index) in designList" :key="index"
                :style="{ paddingBottom: item.brand.length * 40 + 'px' }">

                <div class="table-model" :rowspan="item.brand.length + 1">
                    <figure v-if="!item.model_img || item.model_img.length == 0">
                        <img src="@/assets/images/order/order-ignore.png" alt="">
                    </figure>
                    <CommonTemplatePreview v-else :pieces.sync="item.pieces" :models="item.model_img"
                                           :baseColor="item.colorObject" />
                    <h3 v-text="item.template_name ? item.template_name : item.title"></h3>
                </div>

                <div class="table-info">
                    <p v-if="item.colorObject.color_num && item.sizeObject.size_name">
                        <span>Color :</span>
                        <UxToolTip :content="item.colorObject.color_name" placement="top">
                            <em :style="{ background: item.colorObject.color_num }"></em>
                        </UxToolTip>

                        <br />
                        <span>Size :</span><strong>{{ item.sizeObject.size_name }}</strong>
                    </p>
                    <p v-else>--</p>
                </div>

                <div class="table-design">
                    <p v-if="item.is_blank">N/A</p>
                    <p v-else-if="item.pieces.length == 0">- -</p>
                    <CommonSurfacePreview v-else :design="item.pieces" />
                </div>

                <div class="table-cost">
                    <p v-if="!item.price && !item.extra_price">--</p>
                    <CommonLevelDiscount v-else :blank="item.is_blank" :price="item.price + item.extra_price"
                                         :level="item" type="unit|original|discount" />
                </div>

                <div class="table-cost">
                    <p v-text="item.quantity"></p>
                </div>

                <!-- 代操作订单，忽略第三方 -->
                <template v-if="item.source == 'no'">

                    <!-- 半自定义多一列 -->
                    <div v-if="info.all_custom_count" class="table-cost">--</div>

                    <div class="table-ignored">
                        <button v-if="item.is_ignore" disabled>Ignored</button>
                        <button v-else @click="ignoreShow = item">Neglect</button>
                    </div>
                </template>

                <template v-else>
                    <div class="table-cost">
                        <CommonLevelDiscount :blank="item.is_blank" :price="item.price + item.extra_price"
                                             :count="item.quantity" :level="item" type="unit|original|discount" />
                    </div>

                    <div v-if="info.all_custom_count" class="table-custom">
                        <span v-for="custom in item.customize" :key="custom.id">
                            <a v-text="custom.name" @click="openCustomize(item, custom, true)"></a>
                            <button @click="openCustomize(item, custom, false)" v-if="custom.used">
                                <svg-icon name="order-customize"></svg-icon>
                            </button>
                        </span>
                    </div>

                </template>

                <div v-for="brand in item.brand" :key="brand.type + index + brand.id" class="table-brand">
                    <div></div>
                    <div></div>
                    <div class="table-brand-preview">
                        <CommonBrandPreview :currencyCover="item" :tag="brand.type == 'tag' ? brand : null"
                                            :pack="brand.type == 'pack' ? brand : null" />
                    </div>
                    <CommonLevelDiscount :price="brand.total_price" :level="getBrandLevel(item)"
                                         type="unit|original|discount" />
                    <div class="table-cost" v-text="item.quantity"></div>
                    <CommonLevelDiscount :price="brand.total_price" :count="item.quantity" :level="getBrandLevel(item)"
                                         type="unit|original|discount" />
                </div>

            </li>
        </ul>

        <!-- 待处理订单 忽略第三方 弹窗 -->
        <UxDialog :visible="ignoreShow" @close="ignoreShow = null">
            <div class="dialog-neglect">
                <img src="@/assets/images/order/dialog-ignore.png" alt="">
                <h2>Ignore item</h2>
                <p>The item will not be produced if ignored.</p>
                <button type="primary" @click="setIgnore()">Ignore</button>
                <button @click="ignoreShow = null">Cancel</button>
            </div>
        </UxDialog>
    </div>
</template>
<script>
export default {
    name: "OrderUnpaidProduct",
    props: ["info"],
    data () {
        return {
            table: [],
            loading: false,
            ignoreAll: false,
            ignoreShow: false,
            designList: [],
        };
    },

    watch: {
        'info.design' (val) {
            this.getDesignList(val);
        }
    },

    methods: {
        getDesignList (list) {
            this.designList = list.map(item => {
                const result = { ...item }
                const pieces = Array.isArray(result.design_list) ? result.design_list : JSON.parse(result.design_list || '[]');
                const customize = []
                const sku = item.sku || ''
                pieces.map(piece => {
                    piece.layers = Array.isArray(piece.layers) ? piece.layers : JSON.parse(piece.layers || '[]');
                    piece.layers.map(layer => {
                        if (layer.customize) {
                            customize.push({
                                id: piece.canvas_name + layer.id,
                                name: layer.customize,
                                used: !!layer.customize_used,
                            })
                        }
                    });

                    piece.platens = Array.isArray(piece.platens) ? piece.platens : JSON.parse(piece.platens || '[]');
                })

                result.brand = [];
                result.pieces = pieces;
                result.customize = customize;
                result.sizeObject = { size_name: item.size };
                result.colorObject = {
                    color_id: item.color_id || sku.substring(7, 10) || '1',
                    color_name: item.color_name || '',
                    color_cname: item.color_cname || '',
                    color_num: item.color_num || '',
                    bg_front: item.bg_front || '',
                    bg_back: item.bg_back || '',
                }

                item.tag_design_info && result.brand.push({ ...item.tag_design_info, type: 'tag', tag_name: item.tag_name, tag_model: item.tag_model })
                item.pack_design_info && result.brand.push({ ...item.pack_design_info, type: 'pack', pack_name: item.pack_name, pack_model: item.pack_model })
                return result;
            })

        },
        priceFormat (value) {
            if (value) {
                return "USD " + (value / 100).toFixed(2);
            } else {
                return "--";
            }
        },
        getColorPreview (item) {


            if (item.color_list && item.select_color) {
                // 订单确认页
                const color = item.select_color
                const list = item.color_list || [];
                const select = list.filter((i) => i.color_num == color || i.color_id == color);
                return select[0] || list[0] || {};
            } else if (item.color_num) {
                // 订单详情中的商品列表
                return {
                    color_id: item.color_id || '1',
                    color_name: item.color_name || '',
                    color_cname: item.color_cname || '',
                    color_num: item.color_num || '',
                    bg_front: item.bg_front || '',
                    bg_back: item.bg_back || '',
                }

            } else {
                return {}
            }
        },
        getBrandLevel (item) {
            const now = new Date().getTime()
            if (now > 1719543600000) {
                return item
            } else {
                return { level: 1, user_discount: 100 }
            }
        },

        async setIgnore () {
            if (this.ignoreShow) {
                let item = this.ignoreShow;

                this.loading = true;
                this.ignoreShow = false;
                let response = await this.$http("Order.setIgnore", item.id);

                if (response.code === 0) {
                    this.$emit("update");
                    item.is_ignore = true;
                    this.loading = false;
                } else {
                    this.loading = false;
                    this.$uxMessage.error(response.msg, "Failed to ignore");
                }
            }
        },

        openCustomize (design, custom, upload) {

            const that = this;
            this.$emit('global:ORDER_DESIGN', {
                pieces: JSON.parse(JSON.stringify(design.pieces)),
                color: design.colorObject,
                designId: design.id,
                layerId: custom.id,
                upload: upload,
                callback (result) {
                    design.design_list = result.pieces;
                    design.model_img = result.models;
                    that.getDesignList(that.designList)
                }
            })
        },
    },
    created () {
        this.getDesignList((this.info || {}).design || [])
    }
};
</script>
<style lang="scss" scoped>
.order-design {
    &>h3 {
        display: flex;
        align-items: center;
        font-size: 16px;
        font-weight: 600;
        color: #000000;
        line-height: 18px;
        height: 18px;
        padding: 26px 0 16px;

        span {
            margin-left: 14px;
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #c62828;
            line-height: 18px;
            display: flex;
            align-items: center;

            svg {
                width: 14px;
                height: 14px;
                margin-right: 4px;
            }

            b {
                font-weight: inherit;
            }
        }
    }

    &[custom='custom'] {

        .order-design-title,
        .order-design-list li,
        .table-brand {
            padding-right: 15%;
        }
    }
}

.order-design-title span,
.order-design-list li>div,
.table-brand>div {
    box-sizing: border-box;

    &:nth-child(1) {
        width: 22%;
    }

    &:nth-child(2) {
        width: 15%;
    }

    &:nth-child(3) {
        width: 18%;
    }

    &:nth-child(4) {
        width: 17%;
    }

    &:nth-child(5) {
        width: 11%;
    }

    &:nth-child(6) {
        width: 17%;
    }

    &:nth-child(7) {
        width: 15%;
        position: absolute;
        right: 0;
    }
}

.order-design-title {
    display: flex;
    height: 40px;
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e5e5e5;
    position: relative;

    span {
        box-sizing: border-box;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #292929;
        line-height: 20px;
        padding: 10px;
        text-align: center;
    }
}

.order-design-list {
    position: relative;
    overflow: hidden;

    &>li {
        margin-top: 12px;
        display: flex;
        align-items: center;
        background: #ffffff;
        border-radius: 8px;
        border: 1px solid #e5e5e5;
        position: relative;
    }

    .table-model {
        padding: 12px;

        h3 {
            display: block;
            margin-top: 12px;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #000000;
            line-height: 20px;
            text-align: center;
            max-height: 40px;
            overflow: hidden;
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            line-clamp: 2;
            text-overflow: ellipsis;
            -webkit-line-clamp: 2;
        }

        &>figure {
            position: relative;

            &::before {
                content: '';
                display: block;
                padding-bottom: 100%;
            }

            img {
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
                width: 88px;
                height: 84px;
            }
        }
    }

    .table-info {
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Roboto-Regular, Roboto;
        color: #000000;

        h3 {
            padding: 10px 0;
            font-size: 14px;
            color: #000000;
            font-weight: 500;
            line-height: 16px;
        }

        p {
            display: inline-block;
            text-align: left;
            line-height: 32px;
            font-size: 0;
            color: #000;
            white-space: nowrap;

            span {
                display: inline-block;
                vertical-align: middle;
                width: 43px;
                margin-right: 10px;
                text-align: left;
                font-size: 14px;
                font-weight: 400;
            }

            .tooltip {
                display: inline-block;
                vertical-align: middle;
                width: 24px;
                height: 24px;
            }

            em {
                display: inline-block;
                vertical-align: middle;
                width: 24px;
                height: 24px;
                border-radius: 2px;
                border: 1px solid rgba(0, 0, 0, 0.2);
                box-sizing: border-box;
            }

            strong {
                display: inline-block;
                width: 24px;
                vertical-align: middle;
                font-weight: 400;
                font-size: 14px;
            }
        }
    }

    .table-design {
        font-size: 14px;
        color: #000;
        line-height: 16px;

        &>p {
            text-align: center;
        }
    }

    .table-cost {
        font-size: 14px;
        color: #000000;
        line-height: 16px;
        text-align: center;

        button {
            width: 80px;
            height: 32px;
        }

        .level-discount {
            display: inline-block;
            font-size: 14px;
            white-space: nowrap;

            &::v-deep span[name='discount'] {
                display: block;
                text-align: right;
            }

            &[currency='JPY'] {
                flex-wrap: wrap;

                &::v-deep span {
                    display: block;
                    margin: 2px;
                    text-align: center;
                }
            }

            &[level='1']::v-deep {
                span[name='original'] {
                    display: none;
                }

                span[name='discount'] {
                    display: inline-block;
                }
            }
        }
    }

    .table-thirdsku {
        height: 160px;

        p {
            display: flex;
            flex-direction: column;
            font-size: 14px;
            line-height: 24px;
        }
    }

    .table-custom {
        text-align: center;

        span {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #000000;
            text-decoration: underline;
            line-height: 20px;
            padding: 7px 0;
            white-space: nowrap;

            a {
                display: inline-block;
                max-width: 100px;
                max-height: 60px;
                position: relative;
                white-space: normal;
                word-break: break-word;
                vertical-align: middle;
                text-decoration: underline;
                word-break: break-all;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                overflow: hidden;
                cursor: pointer;

                &:hover {
                    color: #087196;
                }
            }
        }

        button {
            width: 18px;
            height: 18px;
            padding: 1px;
            border: none;
            background: none;
            vertical-align: middle;
            margin-left: 15px;

            svg {
                width: 100%;
                height: 100%;
                fill: #666;
                stroke: #666;
            }

            &:hover svg {
                fill: #ea4c89;
                stroke: #ea4c89;
            }
        }
    }

    .table-ignored {
        display: flex;
        justify-content: center;

        button {
            width: 98px;
            height: 34px;
        }
    }

    .table-brand {
        position: absolute;
        left: 0;
        bottom: 0;
        right: 0;
        width: 100% !important;
        height: 40px;
        display: flex;
        align-items: center;
        background: #faf6eb;
        border-radius: 0 0 8px 8px;
        border-top: 1px solid #f0ebdf;

        &+.table-brand {
            bottom: 40px;
            border-radius: 0;
        }

        &>div {
            &:nth-child(2) {
                width: 0%;
            }

            &:nth-child(3) {
                width: 37%;
            }
        }

        .table-brand-preview {
            display: flex;
            justify-content: flex-end;
            box-sizing: border-box;
            padding: 0 50px 0 30px;
            overflow: hidden;

            .brand-preview {
                background: none;
                width: 100%;
                justify-content: flex-end;

                &::v-deep li {
                    .popper {
                        width: 100%;
                    }

                    .popper-name strong {
                        max-width: 280px;
                    }

                    .level-discount {
                        display: none;
                    }
                }
            }
        }

        .level-discount {
            font-size: 14px;
            align-items: flex-end;
            line-height: 20px;

            &::v-deep span[name='unit'] {
                font-size: 12px;
                line-height: 18px;
            }
        }
    }
}

.dialog-neglect {
    padding: 54px 14px 14px;
    width: 320px;
    height: 414px;
    display: flex;
    align-items: center;
    flex-direction: column;
    box-sizing: border-box;
    text-align: center;

    img {
        width: 80px;
        height: 80px;
    }

    h2 {
        margin-top: 34px;
        height: 40px;
        font-size: 16px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 40px;
    }

    p {
        flex: 1;
        font-size: 16px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        line-height: 22px;
    }

    button {
        width: 100%;
        height: 38px;

        &+button {
            margin-top: 8px;
        }
    }
}
</style>
